#include "login.h"
#include "ui_login.h"
#include <QDebug>
#include <QPushButton>
#include <QFont>
#include "dao/user.h"

//其他页面的依赖
#include "logon.h"
#include "mainwindow.h"

//sql的依赖
#include "sqlutil.h"

//登录处理器
#include "handle/loginhandle.h"

Login::Login(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Login)
{
    ui->setupUi(this);
    initUI();
    initConnect();
}

Login::~Login()
{
    delete ui;
}

void Login::initUI()
{
    //设置窗口图标
    setWindowIcon(QIcon(":/login/login_win_ioc"));
    //设置窗口名字
    setWindowTitle("登录");
    ui->centralWidget->setStyleSheet("background-color:rgba(255, 255, 255,0.6)");
    QFont font("黑体",-1,-1,true);
    ui->pass->setFont(font);
    ui->user->setFont(font);

    //设置登录按钮图标
    ui->login->setIcon(QIcon(":/login/login_btn_ioc"));
    //设置注册按钮图标
    ui->logon->setIcon(QIcon(":/login/logon_btn_ioc"));

    logon = new Logon();


}
void Login::initConnect()
{
    //注册事件
    connect(ui->logon,&QPushButton::clicked,[=](){
        //显示注册页面
        logon->show();
    });
    //登录事件
    connect(ui->login,&QPushButton::clicked,[=](){
        QString user = ui->user_input->text();
        QString pass = ui->pass_input->text();
        //隐藏登录页面
        QSqlQuery query;
        QString sql = "SELECT * FROM user where user=:user";
        query.prepare(sql);
        query.bindValue(":user", user);
        query.exec();
        QString passDb;
        if(query.next()){
            int id = query.value(0).toInt();
            QString userName = query.value(1).toString();
            passDb = query.value(2).toString();
            if(pass.compare(passDb)==0){
                //登录成功
                User user(id,userName);
                user.setId(id);
                user.setUserName(userName);
                LoginHandle::getLoginHandle()->setLoginInfo(true,QDateTime::currentDateTime(),user);
                //刷新主页
                foreach (QWidget *widget, QApplication::allWidgets()){
                    //qDebug()<<widget->windowTitle();//打印出所有的窗口标题，看看有没有我想要的A窗口
                    QString title=widget->windowTitle();//窗口标题
                    if(title=="主页" && widget->isWindow()){
                        this->close();//关闭当前的N界面
                        QLabel *userCenter = widget->findChild<QLabel *>("userCenter");
                        QPushButton *logoutBtn = widget->findChild<QPushButton *>("logout");
                        QPushButton *loginBtn = widget->findChild<QPushButton *>("goLogin");
                        userCenter->setText(userName);
                        userCenter->show();
                        loginBtn->hide();
                        logoutBtn->show();
                        return;
                    }
                }
            }else {
                //密码错误
                qDebug()<<"密码错误";
            }
        }else{
            //账号不存在
            qDebug()<<"账号不存在";
        }
    });

}

